package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.VehicleParkingLotBO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehicleParkingLotBO;
import com.xhwl.logistics.converter.ToPage;
import com.xhwl.logistics.converter.mapstruct.VehicleParkingLotConverter;
import com.xhwl.logistics.dao.mapper.IVehicleParkingLotMapper;
import com.xhwl.logistics.entity.VehicleParkingLot;
import com.xhwl.logistics.service.IVehicleParkingLotService;
import com.xhwl.logistics.service.cent.QueryProjectService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Optional;

//@Service
public class VehicleParkingLotServiceImpl extends ServiceImpl<IVehicleParkingLotMapper, VehicleParkingLot> implements IVehicleParkingLotService {

    @Autowired
    private IVehicleParkingLotMapper vehicleParkingLotMapper;

    @Autowired
    private QueryProjectService queryProjectService;

    @Autowired
    private VehicleParkingLotConverter vehicleParkingLotConverter;

    @Autowired
    private ToPage toPage;

    private Wrapper<VehicleParkingLot> conditionForQueryParkingLot(QueryVehicleParkingLotBO queryVehicleParkingLotBO) {
        LambdaQueryWrapper<VehicleParkingLot> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper.eq(VehicleParkingLot::getProjectId, queryVehicleParkingLotBO.getProjectId());
    }

    private Wrapper<VehicleParkingLot> conditionForQueryParkingLots(Integer projectId, String parkingLotName) {
        LambdaQueryWrapper<VehicleParkingLot> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleParkingLot::getProjectId, projectId)
                .eq(!Strings.isNullOrEmpty(parkingLotName), VehicleParkingLot::getParkingLotName, parkingLotName);
    }

    private Wrapper<VehicleParkingLot> conditionForQueryParkingLot(Integer projectId, String parkingLotName) {
        LambdaQueryWrapper<VehicleParkingLot> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleParkingLot::getProjectId, projectId)
                .eq(VehicleParkingLot::getParkingLotName, parkingLotName);
    }

    private Wrapper<VehicleParkingLot> conditionForQueryParkingLot(Integer projectId) {
        LambdaQueryWrapper<VehicleParkingLot> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleParkingLot::getProjectId, projectId);
    }

    @Override
    public IPage<VehicleParkingLotBO> queryParkingLot(QueryVehicleParkingLotBO queryVehicleParkingLotBO, PageBO pageBO) {

        Page<VehicleParkingLot> vehicleParkingLotPage = vehicleParkingLotMapper.selectPage(pageBO.toPage(VehicleParkingLot.class), conditionForQueryParkingLot(queryVehicleParkingLotBO));

        ProjectBO projectBO = queryProjectService.queryProject(queryVehicleParkingLotBO.getProjectId());

        return toPage.toPage(vehicleParkingLotPage, vehicleParkingLotConverter.toVehicleParkingLotBOList(vehicleParkingLotPage.getRecords(), projectBO.getProjectName()));
    }

    @Override
    public List<VehicleParkingLotSecondBO> queryParkingLots(Integer projectId, String parkingLotName) {
        List<VehicleParkingLot> vehicleParkingLotList = vehicleParkingLotMapper.selectList(conditionForQueryParkingLots(projectId, parkingLotName));
        return vehicleParkingLotConverter.vehicleParkingLotList2VehicleParkingLotSecondBOList(vehicleParkingLotList);
    }

    public VehicleParkingLotSecondBO queryParkingLot(Integer projectId, String parkingLotName) {
        List<VehicleParkingLotSecondBO> vehicleParkingLotSecondBOList = queryParkingLots(projectId, parkingLotName);
        switch (vehicleParkingLotSecondBOList.size()) {
            case 0:
                throw new BusinessException("通过该项目id和车场名称找不到车场，该项目id为" + projectId + "，该车场名称为" + parkingLotName);
            case 1:
                return vehicleParkingLotSecondBOList.get(0);
            default:
                throw new BusinessException("通过该项目id和车场名称找到多个车场，该项目id为" + projectId + "，该车场名称为" + parkingLotName);
        }
    }

    @Override
    public VehicleParkingLotSecondBO queryParkingLot(Integer projectId) {
        VehicleParkingLot vehicleParkingLot = getOne(conditionForQueryParkingLot(projectId));
        return vehicleParkingLotConverter.vehicleParkingLot2VehicleParkingLotSecondBO(vehicleParkingLot);
    }

    @Override
    public Boolean judgeExistParkingLot(Integer projectId, String parkingLotName) {

        VehicleParkingLot vehicleParkingLot = vehicleParkingLotMapper.selectOne(conditionForQueryParkingLot(projectId, parkingLotName));
        return Optional.ofNullable(vehicleParkingLot).isPresent();
    }

    @Override
    public Boolean judgeExistParkingLot(Integer projectId) {
        return !list(conditionForQueryParkingLot(projectId)).isEmpty();
    }
}
